﻿/************************************************
*Author: KIM
*Create Time: 2/11/2017 11:16:48
*CLR Version: 4.0.30319.42000
*Description:
*
*Update History:
*
***********************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ACC.Extensions.Utilities
{
    /// <summary>
    /// 操作App.config的工具类
    /// </summary>
    public class AppConfigHelper
    {
        public static void UpdateAppSettings(string key, string value)
        {
            string file = Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            bool exist = false;
            foreach(string oldKey in config.AppSettings.Settings.AllKeys)
            {
                if(oldKey == key)
                {
                    exist = true;
                    break;
                }
            }
            if(exist)
            {
                config.AppSettings.Settings.Remove(key);
            }
            config.AppSettings.Settings.Add(key, value);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        public static string GetAppSettings(string key)
        {
            string value = String.Empty;
            try
            {
                value = ConfigurationManager.AppSettings[key];
            }
            catch(Exception ex)
            {
                value = String.Empty;
            }
            return value;
        }
    }
}
